package cn.yuan.xiaoyu.caipumodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.caipumodule.adapter.CaiPuTagListAdapter;
import cn.yuan.xiaoyu.caipumodule.adapter.CaipuListAdapter;
import cn.yuan.xiaoyu.caipumodule.bean.CaiPuListBean;
import cn.yuan.xiaoyu.caipumodule.bean.CaiPuTagListBean;
import cn.yuan.xiaoyu.caipumodule.presenter.CaiPuPresenter;
import cn.yuan.xiaoyu.caipumodule.presenter.ICaiPuView;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;
import cn.yuan.xiaoyu.commonmodule.utils.StatusBarCompatUtils;
import cn.yuan.xiaoyu.commonmodule.view.recycleview.YRecycleview;

/**
 * Created by yukuoyuan on 2017/2/28.
 * 这是一个菜谱列表的界面
 */

public class CaiPuActivity extends BaseActivity implements ICaiPuView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toolbar_left)
    ImageView ivToolbarLeft;
    @Bind(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    @Bind(R.id.iv_toolbar_right)
    ImageView ivToolbarRight;
    @Bind(R.id.rcv_caipu_tag)
    RecyclerView rcvCaipuTag;
    @Bind(R.id.rcv_caipu_list)
    YRecycleview rcvCaipuList;
    private CaiPuPresenter caiPuPresenter;
    private int page = 1;
    private int id = 0;
    private CaipuListAdapter caipuListAdapter;
    List<CaiPuListBean.ResultBean.DataBean> listBeens = new ArrayList<>();

    @Override
    protected void bindPresenter() {
        caiPuPresenter = new CaiPuPresenter(this, this);
    }

    @Override
    protected void initData(Bundle extras) {
        showL("加载中");
        rcvCaipuTag.setLayoutManager(new LinearLayoutManager(this));
        rcvCaipuList.setLayoutManager(new LinearLayoutManager(this));
        caiPuPresenter.getTagList();

    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_caipu;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToolbarLeft.setVisibility(View.VISIBLE);
        tvToolbarTitle.setVisibility(View.VISIBLE);
        tvToolbarTitle.setText("菜谱大全");
    }

    @OnClick({R.id.iv_toolbar_left, R.id.iv_toolbar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toolbar_left:
                onBackPressed();
                break;
            case R.id.iv_toolbar_right:
                break;
        }
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void showL(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeL() {
        closeLoad();
        rcvCaipuList.setReFreshComplete();
        rcvCaipuList.loadMoreComplete();
    }

    @Override
    public void setTagListAdapter(CaiPuTagListBean caiPuTagListBean) {
        List<CaiPuTagListBean.ResultBean.ListBean> listBeens = new ArrayList<>();
        for (CaiPuTagListBean.ResultBean.ListBean listBean : caiPuTagListBean.result.get(0).list) {
            listBeens.add(listBean);
        }
        CaiPuTagListAdapter caiPuTagListAdapter = new CaiPuTagListAdapter(listBeens);
        rcvCaipuTag.setAdapter(caiPuTagListAdapter);
        caiPuTagListAdapter.notifyDataSetChanged();
        id = Integer.parseInt(listBeens.get(0).id);
        caiPuPresenter.getCaiPuList(1, id);
    }

    @Override
    public void setCaipuListAdapter(CaiPuListBean caiPuListBean) {
        if (page == 1) {
            listBeens.clear();
        }
        for (CaiPuListBean.ResultBean.DataBean listBean : caiPuListBean.result.data) {
            listBeens.add(listBean);
        }
        if (caipuListAdapter == null) {
            caipuListAdapter = new CaipuListAdapter(listBeens, this);
            rcvCaipuList.setAdapter(caipuListAdapter);
        }
        rcvCaipuList.setRefreshAndLoadMoreListener(this);
        caipuListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        page = 1;
        caiPuPresenter.getCaiPuList(1, id);
    }

    @Override
    public void onLoadMore() {
        page++;
        caiPuPresenter.getCaiPuList(page, id);
    }
}
